package com.wesley.growth.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * CommonIOUtils
 *
 * @author WangPanYong
 * @since 2022/06/20 13:47
 */
public class CommonIOUtils {
    /**
     * 结束标识符
     */
    public static final int EOF = -1;

    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    /**
     * copy
     *
     * @param input 输入流
     * @param output 输出流
     * @return int
     * @author WangPanYong
     * @since 2022/06/20 13:48
     **/
    public static int copy(final InputStream input, final OutputStream output) {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        try {
            while (EOF != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
                count += n;
            }
        } catch (IOException e) {
            // TODO: 2022/10/27 输出日志
            e.printStackTrace();
        }

        if (count > Integer.MAX_VALUE) {
            return -1;
        }
        return (int) count;
    }
}
